// list.js
var api = require('../../api.js');
var app = getApp();
var is_loading_more = false;
var is_no_more = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgServer: api.default.img_server,
    cat_id: "",
    pageSize: 15,
    pageNum: 1,
    listType: "",//列表页的种类，有4种： 新品 new, 热卖 hot, 优选 select, 特惠 discount
    currentListUrl: "",//当前页面的列表页请求地址
    //cat_list: [],
    goods_list: [],
    sort: 4,
    sort_type: -1,
    moneyIcon:api.MONEYICON,
    moneyIconOther:api.MONEYICONOTHER,
  },

  onLoad: function (options) {
    this.loadData(options);
  },

  /**
   * 加载初始数据
   * */
  loadData: function (options) {
    var page = this;
    let listType = "";
    let currentListUrl = '';
    let cat_id = 0;
    if (options.type) {
      listType = options.type;//列表页的种类，有2种： 新品 new, 热卖 hot, 
    }
    if (options.cat_id) {
      cat_id = options.cat_id;//分类id
    }
   
    page.setData({
      listType: listType,
      currentListUrl: currentListUrl,
      //cat_list: cat_list,
      cat_id: options.cat_id || "",
      //height_bar: height_bar,
    });
    page.getGoodsList();

  },
  

  getGoodsList: function () {
    var page = this;
    is_no_more = false;
    // page.setData({
    //     page: 1,
    //     goods_list: [],
    //     show_no_data_tip: false,
    // });
    var cat_id = page.data.cat_id || "";
    var p = page.data.pageSize || 1;
    //wx.showNavigationBarLoading();
    //console.log('page.data.goods_list:'+page.data.sort+','+page.data.sort_type);
    app.request({
      url: api.default.getVirtualList,
      data: {
        // productAttributeCategoryId: cat_id,
        pageNum: page.data.pageNum,
        pageSize: 15,
        // sort1: page.data.sort,
        // order1: page.data.sort_type,
      },
      success: function (res) {
        if (res.code == 200) {
          if (res.data.length == 0) {
            is_no_more = true;
          }
          let tempgoods_list = page.data.goods_list.concat(res.data);
          page.setData({
            page: (p + 1),
            goods_list: tempgoods_list,
            show_no_data_tip: (tempgoods_list.length == 0),
          });
        }
      },
      complete: function () {
        //wx.hideNavigationBarLoading();
      }
    });
  },

  loadMoreGoodsList: function () {
    var page = this;
    if (is_loading_more)
      return;
    page.setData({
      show_loading_bar: true,
    });
    is_loading_more = true;
    var cat_id = page.data.cat_id || "";
    var p = page.data.pageSize || 2;
    app.request({
      url: api.default.goods_list + cat_id,
      data: {
        // productAttributeCategoryId: cat_id,
        pageNum: page.data.pageNum,
        pageSize: 15,
        // sort1: page.data.sort,
        // order1: page.data.sort_type,
      },
      success: function (res) {
        if (res.code == 200) {
          if (res.data.length == 0) {
            is_no_more = true;
          }
          let tempgoods_list = page.data.goods_list.concat(res.data);
          page.setData({
            pageNum: page.data.pageNum + 1,
            goods_list: tempgoods_list,
            show_no_data_tip: (res.data.length == 0),
          });
        }
      },
      complete: function () {
        //wx.hideNavigationBarLoading();
        is_loading_more = false;
        page.setData({
          show_loading_bar: false,
        });
      }
    });
   
  },

  onReachBottom: function () {

  },

  onShow: function (e) {},
   

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
});